import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { getUserOpinidAndToken } from '../util/userInfo';

@Injectable({
	providedIn: 'root'
})

export class BaseHttpService {
	private readonly  BASE_URL = 'http://localhost:8080';
	constructor(private http: HttpClient) { }
	get(url: string): Observable<any> {
		const headers  = new HttpHeaders().set('token', getUserOpinidAndToken().token);
		return this.http.get(`${this.BASE_URL}${url}`, { headers }).pipe(map((response) => this.handleResponse(response)));
	}
	post(url: string, param: any) {
		const headers  = new HttpHeaders().set('token', getUserOpinidAndToken().token);
		return this.http.post(`${this.BASE_URL}${url}`, param, { headers }).pipe(map((response) => this.handleResponse(response)));
	}
	handleResponse(response): Observable<any> {
		if(response['code'] !== 200) {
			return;
		}
		return response['data']['data'];
	}

}
